package cn.ven.controller;

import cn.ven.entity.Book;
import cn.ven.service.BookService;
import cn.ven.service.LargeFileService;
import cn.ven.tool.RespJson;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Controller
@RequestMapping("/api/adm/book")
/*
  管理员("G") 才有权限才可以访问
  对后台书本的增删改查
 */
public class BookController {
    private static Logger log = LoggerFactory.getLogger(BookController.class);

    @Autowired
    BookService bookService;

    @Autowired
    LargeFileService largeFileService;

    //查找所有的书本
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll() {
        List<Book> books = bookService.findAll();
        log.info("books====" + books);
        return RespJson.pack(200, "", books);
    }

    //根据id查找书本
    @RequestMapping("/findById/{id}")
    @ResponseBody
    public Map<String, Object> findById(@PathVariable("id") Integer id) {
        log.info("id===" + id);
        Book book = bookService.findById(id);
        return RespJson.pack(200, "", book);
    }

    //分页查询
    @RequestMapping("/findByPage/{start}/{num}")
    @ResponseBody
    public Map<String, Object> findByPage(@PathVariable("start") Integer start,
                                          @PathVariable("num") Integer num) {
        log.info("start===" + start);
        log.info("num=====" + num);
        Map<String,Object> pageMap =new HashMap<>();  //用于分页查询的集合
        pageMap.put("start",start-1);
        pageMap.put("num",num);
        List<Book> books = bookService.findByPage(pageMap);
        int total = bookService.getCount(0); //查询总条数
        Map<String, Object> map = new HashMap<>();//用Map把对应的书本和书本总条数放入里面
        map.put("books", books);
        map.put("total", total);
        return RespJson.pack(200, "", map);
    }

    //添加或者修改书本
    @RequestMapping("/addBook")
    @ResponseBody
    public Map<String, Object> addBook(Book book,
                                       @RequestParam(value = "pic", required = false) MultipartFile file) throws Exception {
        log.info("bookid===" + book.getId());
        if (book.getId() == null || book.getId() == 0) { //bookid不存在的时候，也就是添加
            String id = largeFileService.add(file);
            book.setPicture(id);//把图片id放入到书本图片字段绑定
            bookService.addBook(book);
            log.info("book===" + book);
            List<Book> books = bookService.findAll();//返回给前端一个新的书本集合
            return RespJson.pack(200, "添加成功", books);
        }
        //更新书本
        if (book.getId() != null && book.getId() != 0) {
            largeFileService.delLarge(book.getPicture());//先删除原来书本有的图片
            String id = largeFileService.add(file);
            book.setPicture(id);//把图片id放入到书本图片字段绑定
            bookService.updateBook(book);
            List<Book> books = bookService.findAll();//返回给前端一个新的书本集合
            return RespJson.pack(200, "修改成功", books);
    }
            List<Book> books = bookService.findAll();//返回给前端一个新的书本集合
            return RespJson.pack(500,"出现异常，操作失误",books);
    }


}
